--
-- Author:      feilong
-- DateTime:    2019-10-30 10:59:15
-- Description: 集群管理

local skynet = require "skynet"
local cluster = require "skynet.cluster"


local ClusterManager = class("ClusterManager")

---------------------------------------------------------
-- Private
---------------------------------------------------------
function ClusterManager:ctor(message_dispatch)
	self.message_dispatch = message_dispatch
	self.node_cluster_id = skynet.getenv("node_cluster_id")		
	self.cluster_list = {} --集群列表
    self.server_list = {} --服务器节点列表
	self:register()
end

function ClusterManager:register()
	self.message_dispatch:registerSelf('cluster_heart_req', handler(self,self.cluseterHeartReq))
	self.message_dispatch:registerSelf('cluster_update_nt', handler(self,self.cluseterUpdateNt))
end

--打开自己的端口
function ClusterManager:open(server_info)	
	self.node_message = global.node_message
	self.server_info = server_info
    self.server_name = server_info:getName()
	self:addServer(server_info)
	cluster.open(self.server_name)
    if not self.server_name:find("nodeserver") then 
        print("____self.server_name__", self.server_name)
        self:serverRegister()
    end
end

--更新集群
function ClusterManager:updateCluster(server_data)
    -- 重新加载集群配置
    local name = server_data.name
    local cluster_addr = server_data.cluster_addr 
    self.server_list[name] = server_data
    self.cluster_list[name] = cluster_addr
    
end

function ClusterManager:updateClusterList(cluster_list)   
    for k, v in pairs(cluster_list) do 
	    self.cluster_list[k] = v
	end    
    cluster.reload(self.cluster_list)
end

--返回集群列表
function ClusterManager:getClusterList()
	return self.cluster_list
end

function ClusterManager:addServer(server_info)   
    -- 重新加载集群配置
    local name = server_info:getName()
    self.cluster_list[name] = server_info:getClusterAddr() 
    self.server_list[name] = server_info:getData() 
    cluster.reload(self.cluster_list)
end

function ClusterManager:removeServer(server_name)   
    -- 重新加载集群配置
    self.cluster_list[server_name] = nil        
    self.server_list[server_name] = nil        
    cluster.reload(self.cluster_list)
end

-- 向clusterServer注册
function ClusterManager:serverRegister()
    self.node_message:callNodeManager(self.node_cluster_id, "server_register_req", self.server_info:getData()) 
end

--获取服务器信息
function ClusterManager:getServer(name)
    return self.server_list[name]
end

--获取服务器列表
function ClusterManager:getServerList()
    return self.server_list
end

--获取服务器信息
function ClusterManager:getServerByType(server_type)    
    local server_list = {}
    for _, v in pairs(self.server_list) do         
        if v.type == server_type then 
            table_insert(server_list, v)
        end
    end    
    return server_list
end


---------------------------------------------------------
-- CMD
---------------------------------------------------------
--nodeserver发来的心跳请求
function ClusterManager:cluseterHeartReq(msg_data)
	-- print("_________cluseterHeartReq______", msg_data)
	return true
end

--通知更新集群列表
function ClusterManager:cluseterUpdateNt(msg_data)
	-- debug_log("_________cluseterUpdateNt______", msg_data)
	-- self:updateClusterList(msg_data)
    for k, v in pairs(msg_data) do 
        self:updateCluster(v)
    end
    if next(msg_data) then 
        cluster.reload(self.cluster_list)
    end

	return true
end


return ClusterManager